*************************************************************************
*																		*
*				Campaign Panel Book Do-File Table 7						*
*																		*
**************************************************************************

* Dummy talks with conversation partners with particular party yes/no
foreach year in a b {
foreach welle of numlist 2/7 {
gen `year'_gesprcdu`welle' = 1 if `year'_wabsfull_gp`welle'==1
replace `year'_gesprcdu`welle' = 0 if `year'_wabsfull_gp`welle'>1 & ///
	`year'_wabsfull_gp`welle'<=999
replace `year'_gesprcdu`welle' = 0 if `year'_talk`welle'==0

gen `year'_gesprspd`welle' = 1 if `year'_wabsfull_gp`welle'==4
replace `year'_gesprspd`welle' = 0 if `year'_wabsfull_gp`welle'~=4 & ///
	`year'_wabsfull_gp`welle'~=.
replace `year'_gesprspd`welle' = 0 if `year'_talk`welle'==0

gen `year'_gesprfdp`welle' = 1 if `year'_wabsfull_gp`welle'==5
replace `year'_gesprfdp`welle' = 0 if `year'_wabsfull_gp`welle'~=5 & ///
	`year'_wabsfull_gp`welle'~=.
replace `year'_gesprfdp`welle' = 0 if `year'_talk`welle'==0
	
gen `year'_gesprgru`welle' = 1 if `year'_wabsfull_gp`welle'==6
replace `year'_gesprgru`welle' = 0 if `year'_wabsfull_gp`welle'~=6 & ///
	`year'_wabsfull_gp`welle'~=.
replace `year'_gesprgru`welle' = 0 if `year'_talk`welle'==0

gen `year'_gesprlink`welle' = 1 if `year'_wabsfull_gp`welle'==7
replace `year'_gesprlink`welle' = 0 if `year'_wabsfull_gp`welle'~=7 & ///
	`year'_wabsfull_gp`welle'~=.
replace `year'_gesprlink`welle' = 0 if `year'_talk`welle'==0
}
}

estimates clear
foreach year in a b {
foreach partei in cdu spd fdp gru link {
foreach welle of numlist 2/7 {
 logit `year'_gespr`partei'`welle' b808.`year'_pidfull1  `year'_polint2 ///
	if !missing(`year'_gespr`partei'2, `year'_gespr`partei'3, ///
				`year'_gespr`partei'4, `year'_gespr`partei'5, ///
				`year'_gespr`partei'6, `year'_gespr`partei'7) & ///
				`year'_pidfull1~=801 [pw=`year'_weight]
				
margins `year'_pidfull1, post atmeans

estimates store `year'_point`partei'`welle'


capture gen `year'_help`partei'`welle'1 = _b[1.`year'_pidfull1] if e(sample)
capture gen `year'_help`partei'`welle'4 = _b[4.`year'_pidfull1] if e(sample)
capture gen `year'_help`partei'`welle'5 = _b[5.`year'_pidfull1] if e(sample)
capture gen `year'_help`partei'`welle'6 = _b[6.`year'_pidfull1] if e(sample)
capture gen `year'_help`partei'`welle'7 = _b[7.`year'_pidfull1] if e(sample)
capture gen `year'_help`partei'`welle'808 = _b[808.`year'_pidfull1] if e(sample)

}
}
}

//fre a_helplink21		//perfect prediction for CDU/CSU-identifiers Linke-discussant = 0 
gen a_helplink21 =0 


fre a_helpcdu21 
/* spelled out party name indicates party of the dependent variable,
first number indicates the wave and second number indicates the party of 
the independent variable*/


* Aggregation of wave means
foreach year in a b {
foreach partei in cdu spd fdp gru link {

gen `year'_av`partei'1 = (`year'_help`partei'21 + `year'_help`partei'31 + `year'_help`partei'41 + `year'_help`partei'51 + `year'_help`partei'61 + `year'_help`partei'71)/6
label var `year'_av`partei'1 "`partei'"
gen `year'_av`partei'4 = (`year'_help`partei'24 + `year'_help`partei'34 + `year'_help`partei'44 + `year'_help`partei'54 + `year'_help`partei'64 + `year'_help`partei'74)/6
label var `year'_av`partei'4 "`partei'"
gen `year'_av`partei'5 = (`year'_help`partei'25 + `year'_help`partei'35 + `year'_help`partei'45 + `year'_help`partei'55 + `year'_help`partei'65 + `year'_help`partei'75)/6
label var `year'_av`partei'5 "`partei'"
gen `year'_av`partei'6 = (`year'_help`partei'26 + `year'_help`partei'36 + `year'_help`partei'46 + `year'_help`partei'56 + `year'_help`partei'66 + `year'_help`partei'76)/6
label var `year'_av`partei'6 "`partei'"
gen `year'_av`partei'7 = (`year'_help`partei'27 + `year'_help`partei'37 + `year'_help`partei'47 + `year'_help`partei'57 + `year'_help`partei'67 + `year'_help`partei'77)/6
label var `year'_av`partei'7 "`partei'"
gen `year'_av`partei'808 = (`year'_help`partei'2808 + `year'_help`partei'3808 + `year'_help`partei'4808 + `year'_help`partei'5808 + `year'_help`partei'6808 + `year'_help`partei'7808)/6
label var `year'_av`partei'808 "`partei'"
}
}

fre a_avcdu1 //spelled out party = party DV, number = party IV

* Standard deviation
foreach year in a b {
foreach partei in cdu spd fdp gru link {
foreach uv in 1 4 5 6 7 808 {

gen `year'_sd`partei'`uv' = sqrt((((`year'_help`partei'2`uv' - `year'_av`partei'`uv')^2)+((`year'_help`partei'3`uv' - `year'_av`partei'`uv')^2)+((`year'_help`partei'4`uv' - `year'_av`partei'`uv')^2) ///
								+((`year'_help`partei'5`uv' - `year'_av`partei'`uv')^2)+((`year'_help`partei'6`uv' - `year'_av`partei'`uv')^2)+((`year'_help`partei'7`uv' - `year'_av`partei'`uv')^2))/6)

					
}
}
}


* Table

estimates clear
foreach year in a b {
foreach partei in cdu spd fdp gru link {

mean `year'_av`partei'*
estimates store `year'_av`partei'

foreach uv in 1 4 5 6 7 808 {
sum `year'_sd`partei'`uv'
estadd scalar `year'_std`partei'`uv' = r(mean) , replace

}
}
}


esttab a_avcdu a_avspd a_avfdp a_avgru a_avlink ///
	using "Tables/nice table2.rtf", replace ///
	b(2) not nonumb stats(a_stdcdu1 a_stdspd1 a_stdfdp1 a_stdgru1 a_stdlink1 ///
		a_stdcdu4 a_stdspd4 a_stdfdp4 a_stdgru4 a_stdlink4 ///
		a_stdcdu5 a_stdspd5 a_stdfdp5 a_stdgru5 a_stdlink5 ///
		a_stdcdu6 a_stdspd6 a_stdfdp6 a_stdgru6 a_stdlink6 ///
		a_stdcdu7 a_stdspd7 a_stdfdp7 a_stdgru7 a_stdlink7 ///
		a_stdcdu808 a_stdspd808 a_stdfdp808 a_stdgru808 a_stdlink808 N) 	
	
esttab b_avcdu b_avspd b_avfdp b_avgru b_avlink ///
	using "Tables/nice table2.rtf", append ///
	b(2) not nonumb stats(b_stdcdu1 b_stdspd1 b_stdfdp1 b_stdgru1 b_stdlink1 ///
		b_stdcdu4 b_stdspd4 b_stdfdp4 b_stdgru4 b_stdlink4 ///
		b_stdcdu5 b_stdspd5 b_stdfdp5 b_stdgru5 b_stdlink5 ///
		b_stdcdu6 b_stdspd6 b_stdfdp6 b_stdgru6 b_stdlink6 ///
		b_stdcdu7 b_stdspd7 b_stdfdp7 b_stdgru7 b_stdlink7 ///
		b_stdcdu808 b_stdspd808 b_stdfdp808 b_stdgru808 b_stdlink808 N) 	


save "${data}\data2009_2013 participants 7 waves.dta", replace
